#include "commond.hpp"


//进程间通信
//server端创建共享内存，并接受信息
int main()
{
    //创造shm
    key_t k = GetKey();
    printf("key: 0x%x\n", k);
    int shmid = CreateShm(k);
    std::cout << "shmId:" << shmid << std::endl;


    //与共享内存建立联系
    char* start = (char*)AttachShm(shmid);

    
    //获取共享内存属性
    shmid_ds sd;
    shmctl(shmid, IPC_STAT, &sd);
    std::cout << "shm_key: " << sd.shm_perm.__key << " shm_size: " << sd.shm_segsz << std::endl;
    
    //通信
    while(true)
    {
        std::cout << "client say:";
        std::cout << start << std::endl;
        sleep(1);
    }
    // shmid_ds ds;
    // shmctl(shmid, IPC_STAT, &ds);
    // printf("key: 0x%x\n", ds.shm_perm.__key);
    // printf("pid: %d, shm_segsz: %d, cpid: %d\n", getpid(), ds.shm_segsz, ds.shm_cpid);

    DetachShm(start);

    
    //回收共享内存
    int rcv = shmctl(shmid, IPC_RMID, nullptr);
    if(rcv == -1)
    {
        std::cerr << "rmid" << errno << strerror(errno) << std::endl;
        exit(2);  
    }

    return 0;
}